package treazy.ultimate.stats;

import java.util.ArrayList;
import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ViewRosterActivity extends ListActivity {
	private String teamName;
	private ArrayList<Player> players;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
	    
	    Bundle extras = getIntent().getExtras();
	    if (extras != null) teamName = extras.getString("TeamName");
	    
	    DatabaseHandler db = new DatabaseHandler(this);
	    ArrayList<String> playerNames = new ArrayList<String>();
	    if(teamName != null) {
	    	players = db.getRoster(teamName);
	    	for(Player p: players) {
	    		playerNames.add(p.getName());
	    	}
	    	if(players.isEmpty()) {
	    		playerNames.add("No Players");
	    		players.add(new Player("No Players", null));
	    	}
	    } else { 
	    	playerNames.add("No Players");
	    	players.add(new Player("No Players", null));
	    }
	    
		setListAdapter(new ArrayAdapter<String>(this, R.layout.viewroster, R.id.rosterlist, playerNames));
		ListView listView = getListView();
		listView.setTextFilterEnabled(true);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(players.get(0).getName() != "No Players") {
					// TODO: View player page
				
//					Object selectedTeam = parent.getItemAtPosition(position);
//					Intent i = new Intent(getApplicationContext(), TeamActivity.class);
//					i.putExtra("teamName", selectedTeam.toString());
//					startActivity(i);
				}
			}
		});
	}
	
	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.playersmenu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.newplayer:
	        	Intent i = new Intent(getApplicationContext(), NewPlayerActivity.class);
	        	i.putExtra("TeamName", teamName);
	        	startActivity(i);
	            return true;
	        case android.R.id.home:
	        	Intent i1 = new Intent(getApplicationContext(), TeamActivity.class);
	        	i1.putExtra("TeamName", teamName);
	        	i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        	startActivity(i1);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

}
